package com.itluma.isWOak.controller;

import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;

/**
 * @author MS
 * @date 2021/4/7 17:09
 **/
@RequestMapping("/admin")
@Controller
public class AdminController{


    @GetMapping("/login")
    public String login(){

        return "login";
    }

    @PostMapping("/login")
    public ModelAndView check(HttpServletRequest request){

       String exceptionClassName = (String) request.getAttribute ("shiroLoginFailure");
        System.out.println ("登录验证中....");
        System.out.println (1 / 0);
        String error = "";
        if (UnknownAccountException.class.getName ().equalsIgnoreCase (exceptionClassName)) {
            error = "用户名不存在!";
        } else if (IncorrectCredentialsException.class.getName ().equalsIgnoreCase (exceptionClassName)) {
            error = "密码错误!";
        } else if (exceptionClassName != null) {
            error = "未知错误!";
        }
        ModelAndView mv = new ModelAndView ();
        mv.setViewName ("login");
        mv.addObject ("error", error);
        return mv;
    }

}